//--------------在线下单 进口

//--------------添加集装箱
var boxSerIdIndex = 0;
var box_edit_serid = "";
function setBoxSerIdIndex(value) {
    if (value) {
        boxSerIdIndex = value;
    }
}
function addOrUpdateBox() {
    if (box_edit_serid != null && box_edit_serid != "") {
        updateBoxTr(box_edit_serid);
    } else {
        addBox();
    }
}

function checkRepeatJZXH() {
    var flag = 1;
    if ($("#box_jzxh").val() != '') {
        $("[id^='box_jzxh_']").not("[id='box_jzxh_" + box_edit_serid + "']").each(function () {
            //alert($(this).attr("id")+":"+$(this).val()+" "+box_edit_serid);
            if ($(this).val() == $("#box_jzxh").val()) {
                flag = 0;
                //	alert("集装箱号不能重复");
                $.messager.alert('提示', "集装箱号不能重复 ");
                $("#box_jzxh").focus();
            }
        });
    } else {
        flag = 0;
    }
    return flag;
}

function addBox() {
    var box_jzxh = $("#box_jzxh").val();
    var box_jzxgg = $("#box_jzxgg").val();
    var box_jzxggName = $("#box_jzxggName").val();
    var box_jzxzz = $("#box_jzxzz").val();
    if (box_jzxh == "") {
        //    alert("集装箱编号不能为空！");
        $.messager.alert('提示', "集装箱编号不能为空！ ");
        $("#box_jzxh").focus();
        return;
    } else if (box_jzxgg == "") {
        //    alert("集装箱规格不能为空！");
        $.messager.alert('提示', "集装箱规格不能为空！ ");
        focusJzxgg();
        return;
    }
    if (checkRepeatJZXH() != 1) {
        return;
    }
    var addHtml = getNewHgBoxHtml(box_jzxh, box_jzxgg, box_jzxggName, box_jzxzz);
    boxSerIdIndex++;
    $("#box_lastTR").before(addHtml);
    $("#box_jzxh").val("");
    $("#box_id").val("");
    $("#box_jzxh").focus();
    countBoxTr();
}

function editBoxTr(trSerNo) {
    unlockJzx();
    box_edit_serid = "" + trSerNo;
    var value = $("#box_jzxgg_" + trSerNo).val();
    setSelectValue($("#jzxggselect_123"), "box_jzxgg", "box_jzxggName", "box_jzxgg" + "_" + trSerNo,
                   "box_jzxggName" + "_" + trSerNo);
//    initSelectValueS($("#jzxggselect_123"),value);
    editTr("box_TR" + trSerNo);
    $("#box_addButton").val("修改");
    $("#box_jzxh").focus();
    lockJzx();
}

function updateBoxTr() {
    var box_jzxh = $("#box_jzxh").val();
    var box_jzxgg = $("#box_jzxgg").val();

    if (box_jzxh == "") {
        //    alert("集装箱编号不能为空！");
        $.messager.alert('提示', "集装箱编号不能为空！");
        $("#box_jzxh").focus();
        return;
    } else if (box_jzxgg == "") {
        //    alert("集装箱规格不能为空！");
        $.messager.alert('提示', "集装箱规格不能为空！！");
        focusJzxgg()
        return;
    }
    if (checkRepeatJZXH() != 1) {
        return;
    }
    updateTr("box_input_table", box_edit_serid);

    $("#box_jzxh").val("");
    $("#box_id").val("");
    $("#box_jzxh").focus();
    initBoxInput();
}

function focusJzxgg() {
    focusDemo($("#jzxggselect_123"));

}
function initBoxInput() {
    box_edit_serid = "";
    $("#box_addButton").val("添加");
}
var boxPreName = "baHgbaseinfo.hgBox";//  关键界面 值 "baHgbaseinfo.baHggoods";

function setBoxPreName(value) {
    boxPreName = value;
}

function getNewHgBoxHtml(box_jzxh, box_jzxgg, box_jzxggName, box_jzxzz) {
    var idPrefixName = "box";
    var boxName = boxPreName + "[" + boxSerIdIndex + "]";
    var result = "<tr id='" + idPrefixName + "_TR" + boxSerIdIndex + "' onclick='editBoxTr("
                 + boxSerIdIndex + ")'>";
    result += "<td>";
    result += getInput(idPrefixName + "_id_" + boxSerIdIndex, boxName + ".id", "", 1, "width:100%");
    result +=
        getInput(idPrefixName + "_jzxh_" + boxSerIdIndex, boxName + ".jzxh", box_jzxh, 0,
                 "width:100%");
    result += "</td><td>";
    result +=
        getInput(idPrefixName + "_jzxgg_" + boxSerIdIndex, boxName + ".jzxgg", box_jzxgg, 1,
                 "width:100%");
    result +=
        getInput(idPrefixName + "_jzxggName_" + boxSerIdIndex, "", box_jzxggName, 0, "width:100%");
    result += "</td><td>";
    result +=
        getInput(idPrefixName + "_jzxzz_" + boxSerIdIndex, boxName + ".jzxzz", box_jzxzz + "", 0,
                 "width:100%");
    result += "</td><td>";
    result +=
        "<img src='../../cargodecl/images/delete.gif' onclick='delBoxTr(\"" + idPrefixName + "_TR"
        + boxSerIdIndex
        + "\")'/></td>";
    result += "</tr>";
    return result;
}

function delBoxTr(trId) {
    var hglock = $("#HGISLOCK").val();
    var jylock = $("#JYISLOCK").val();
    if (hglock == "1" || jylock == "1") {
        $.messager.alert('提示', "该数据已经锁住不能删除");
        return;
    }
    $("#" + trId).remove();
    initBoxInput();
    $("#box_jzxh").focus();
    countBoxTr();
    if (event.stopPropagation) {
        event.stopPropagation();
    } else {
        event.cancelBubble = true;
    }
}

function setHgBoxSerId() {
    var hgBoxStr = "";
    $(".hgBoxSerId").each(function () {
        var value = $(this).val();
        hgBoxStr += value + ",";
    });
    hgBoxStr = hgBoxStr.substring(0, hgBoxStr.length - 1);
    $("#hgBox").val(hgBoxStr);
}

function editBox(boxSerIdIndex) {
    var box_jzxh = $("#hgBox_jzxh_" + boxSerIdIndex).val();
    var box_jzxgg = $("#hgBox_jzxgg_" + boxSerIdIndex).val();
    var box_jzxzz = $("#hgBox_jzxzz_" + boxSerIdIndex).val();

    $("#box_jzxh").val(box_jzxh);
    $("#box_jzxgg").val(box_jzxgg);
    $("#box_jzxzz").val(box_jzxzz);
    $("#box_jzxh").focus();
}
